<?php


abstract class BaseCategories extends BaseObject  implements Persistent {


  const PEER = 'CategoriesPeer';

	
	protected static $peer;

	
	protected $id;

	
	protected $name;

	
	protected $parentid;

	
	protected $image;

	
	protected $aCategoriesRelatedByParentid;

	
	protected $collCategoriessRelatedByParentid;

	
	private $lastCategoriesRelatedByParentidCriteria = null;

	
	protected $collProductss;

	
	private $lastProductsCriteria = null;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

	
	public function __construct()
	{
		parent::__construct();
		$this->applyDefaultValues();
	}

	
	public function applyDefaultValues()
	{
	}

	
	public function getId()
	{
		return $this->id;
	}

	
	public function getName()
	{
		return $this->name;
	}

	
	public function getParentid()
	{
		return $this->parentid;
	}

	
	public function getImage()
	{
		return $this->image;
	}

	
	public function setId($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->id !== $v) {
			$this->id = $v;
			$this->modifiedColumns[] = CategoriesPeer::ID;
		}

		return $this;
	} 
	
	public function setName($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->name !== $v) {
			$this->name = $v;
			$this->modifiedColumns[] = CategoriesPeer::NAME;
		}

		return $this;
	} 
	
	public function setParentid($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->parentid !== $v) {
			$this->parentid = $v;
			$this->modifiedColumns[] = CategoriesPeer::PARENTID;
		}

		if ($this->aCategoriesRelatedByParentid !== null && $this->aCategoriesRelatedByParentid->getId() !== $v) {
			$this->aCategoriesRelatedByParentid = null;
		}

		return $this;
	} 
	
	public function setImage($v)
	{
								if (!is_resource($v)) {
			$this->image = fopen('php://memory', 'r+');
			fwrite($this->image, $v);
			rewind($this->image);
		} else { 			$this->image = $v;
		}
		$this->modifiedColumns[] = CategoriesPeer::IMAGE;

		return $this;
	} 
	
	public function hasOnlyDefaultValues()
	{
						if (array_diff($this->modifiedColumns, array())) {
				return false;
			}

				return true;
	} 
	
	public function hydrate($row, $startcol = 0, $rehydrate = false)
	{
		try {

			$this->id = ($row[$startcol + 0] !== null) ? (string) $row[$startcol + 0] : null;
			$this->name = ($row[$startcol + 1] !== null) ? (string) $row[$startcol + 1] : null;
			$this->parentid = ($row[$startcol + 2] !== null) ? (string) $row[$startcol + 2] : null;
			if ($row[$startcol + 3] !== null) {
				$this->image = fopen('php://memory', 'r+');
				fwrite($this->image, $row[$startcol + 3]);
				rewind($this->image);
			} else {
				$this->image = null;
			}
			$this->resetModified();

			$this->setNew(false);

			if ($rehydrate) {
				$this->ensureConsistency();
			}

						return $startcol + 4; 
		} catch (Exception $e) {
			throw new PropelException("Error populating Categories object", $e);
		}
	}

	
	public function ensureConsistency()
	{

		if ($this->aCategoriesRelatedByParentid !== null && $this->parentid !== $this->aCategoriesRelatedByParentid->getId()) {
			$this->aCategoriesRelatedByParentid = null;
		}
	} 
	
	public function reload($deep = false, PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("Cannot reload a deleted object.");
		}

		if ($this->isNew()) {
			throw new PropelException("Cannot reload an unsaved object.");
		}

		if ($con === null) {
			$con = Propel::getConnection(CategoriesPeer::DATABASE_NAME, Propel::CONNECTION_READ);
		}

				
		$stmt = CategoriesPeer::doSelectStmt($this->buildPkeyCriteria(), $con);
		$row = $stmt->fetch(PDO::FETCH_NUM);
		$stmt->closeCursor();
		if (!$row) {
			throw new PropelException('Cannot find matching row in the database to reload object values.');
		}
		$this->hydrate($row, 0, true); 
		if ($deep) {  
			$this->aCategoriesRelatedByParentid = null;
			$this->collCategoriessRelatedByParentid = null;
			$this->lastCategoriesRelatedByParentidCriteria = null;

			$this->collProductss = null;
			$this->lastProductsCriteria = null;

		} 	}

	
	public function delete(PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("This object has already been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(CategoriesPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}
		
		$con->beginTransaction();
		try {
			CategoriesPeer::doDelete($this, $con);
			$this->setDeleted(true);
			$con->commit();
		} catch (PropelException $e) {
			$con->rollBack();
			throw $e;
		}
	}

	
	public function save(PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("You cannot save an object that has been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(CategoriesPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}
		
		$con->beginTransaction();
		try {
			$affectedRows = $this->doSave($con);
			$con->commit();
			CategoriesPeer::addInstanceToPool($this);
			return $affectedRows;
		} catch (PropelException $e) {
			$con->rollBack();
			throw $e;
		}
	}

	
	protected function doSave(PropelPDO $con)
	{
		$affectedRows = 0; 		if (!$this->alreadyInSave) {
			$this->alreadyInSave = true;

												
			if ($this->aCategoriesRelatedByParentid !== null) {
				if ($this->aCategoriesRelatedByParentid->isModified() || $this->aCategoriesRelatedByParentid->isNew()) {
					$affectedRows += $this->aCategoriesRelatedByParentid->save($con);
				}
				$this->setCategoriesRelatedByParentid($this->aCategoriesRelatedByParentid);
			}


						if ($this->isModified()) {
				if ($this->isNew()) {
					$pk = CategoriesPeer::doInsert($this, $con);
					$affectedRows += 1; 										 										 
					$this->setNew(false);
				} else {
					$affectedRows += CategoriesPeer::doUpdate($this, $con);
				}

								if ($this->image !== null && is_resource($this->image)) {
					rewind($this->image);
				}

				$this->resetModified(); 			}

			if ($this->collCategoriessRelatedByParentid !== null) {
				foreach ($this->collCategoriessRelatedByParentid as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			if ($this->collProductss !== null) {
				foreach ($this->collProductss as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			$this->alreadyInSave = false;

		}
		return $affectedRows;
	} 
	
	protected $validationFailures = array();

	
	public function getValidationFailures()
	{
		return $this->validationFailures;
	}

	
	public function validate($columns = null)
	{
		$res = $this->doValidate($columns);
		if ($res === true) {
			$this->validationFailures = array();
			return true;
		} else {
			$this->validationFailures = $res;
			return false;
		}
	}

	
	protected function doValidate($columns = null)
	{
		if (!$this->alreadyInValidation) {
			$this->alreadyInValidation = true;
			$retval = null;

			$failureMap = array();


												
			if ($this->aCategoriesRelatedByParentid !== null) {
				if (!$this->aCategoriesRelatedByParentid->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->aCategoriesRelatedByParentid->getValidationFailures());
				}
			}


			if (($retval = CategoriesPeer::doValidate($this, $columns)) !== true) {
				$failureMap = array_merge($failureMap, $retval);
			}


				if ($this->collCategoriessRelatedByParentid !== null) {
					foreach ($this->collCategoriessRelatedByParentid as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}

				if ($this->collProductss !== null) {
					foreach ($this->collProductss as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}


			$this->alreadyInValidation = false;
		}

		return (!empty($failureMap) ? $failureMap : true);
	}

	
	public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = CategoriesPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		$field = $this->getByPosition($pos);
		return $field;
	}

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getId();
				break;
			case 1:
				return $this->getName();
				break;
			case 2:
				return $this->getParentid();
				break;
			case 3:
				return $this->getImage();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true)
	{
		$keys = CategoriesPeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getId(),
			$keys[1] => $this->getName(),
			$keys[2] => $this->getParentid(),
			$keys[3] => $this->getImage(),
		);
		return $result;
	}

	
	public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = CategoriesPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->setByPosition($pos, $value);
	}

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setId($value);
				break;
			case 1:
				$this->setName($value);
				break;
			case 2:
				$this->setParentid($value);
				break;
			case 3:
				$this->setImage($value);
				break;
		} 	}

	
	public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = CategoriesPeer::getFieldNames($keyType);

		if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setName($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setParentid($arr[$keys[2]]);
		if (array_key_exists($keys[3], $arr)) $this->setImage($arr[$keys[3]]);
	}

	
	public function buildCriteria()
	{
		$criteria = new Criteria(CategoriesPeer::DATABASE_NAME);

		if ($this->isColumnModified(CategoriesPeer::ID)) $criteria->add(CategoriesPeer::ID, $this->id);
		if ($this->isColumnModified(CategoriesPeer::NAME)) $criteria->add(CategoriesPeer::NAME, $this->name);
		if ($this->isColumnModified(CategoriesPeer::PARENTID)) $criteria->add(CategoriesPeer::PARENTID, $this->parentid);
		if ($this->isColumnModified(CategoriesPeer::IMAGE)) $criteria->add(CategoriesPeer::IMAGE, $this->image);

		return $criteria;
	}

	
	public function buildPkeyCriteria()
	{
		$criteria = new Criteria(CategoriesPeer::DATABASE_NAME);

		$criteria->add(CategoriesPeer::ID, $this->id);

		return $criteria;
	}

	
	public function getPrimaryKey()
	{
		return $this->getId();
	}

	
	public function setPrimaryKey($key)
	{
		$this->setId($key);
	}

	
	public function copyInto($copyObj, $deepCopy = false)
	{

		$copyObj->setId($this->id);

		$copyObj->setName($this->name);

		$copyObj->setParentid($this->parentid);

		$copyObj->setImage($this->image);


		if ($deepCopy) {
									$copyObj->setNew(false);

			foreach ($this->getCategoriessRelatedByParentid() as $relObj) {
				if ($relObj !== $this) {  					$copyObj->addCategoriesRelatedByParentid($relObj->copy($deepCopy));
				}
			}

			foreach ($this->getProductss() as $relObj) {
				if ($relObj !== $this) {  					$copyObj->addProducts($relObj->copy($deepCopy));
				}
			}

		} 

		$copyObj->setNew(true);

	}

	
	public function copy($deepCopy = false)
	{
				$clazz = get_class($this);
		$copyObj = new $clazz();
		$this->copyInto($copyObj, $deepCopy);
		return $copyObj;
	}

	
	public function getPeer()
	{
		if (self::$peer === null) {
			self::$peer = new CategoriesPeer();
		}
		return self::$peer;
	}

	
	public function setCategoriesRelatedByParentid(Categories $v = null)
	{
		if ($v === null) {
			$this->setParentid(NULL);
		} else {
			$this->setParentid($v->getId());
		}

		$this->aCategoriesRelatedByParentid = $v;

						if ($v !== null) {
			$v->addCategoriesRelatedByParentid($this);
		}

		return $this;
	}


	
	public function getCategoriesRelatedByParentid(PropelPDO $con = null)
	{
		if ($this->aCategoriesRelatedByParentid === null && (($this->parentid !== "" && $this->parentid !== null))) {
			$c = new Criteria(CategoriesPeer::DATABASE_NAME);
			$c->add(CategoriesPeer::ID, $this->parentid);
			$this->aCategoriesRelatedByParentid = CategoriesPeer::doSelectOne($c, $con);
			
		}
		return $this->aCategoriesRelatedByParentid;
	}

	
	public function clearCategoriessRelatedByParentid()
	{
		$this->collCategoriessRelatedByParentid = null; 	}

	
	public function initCategoriessRelatedByParentid()
	{
		$this->collCategoriessRelatedByParentid = array();
	}

	
	public function getCategoriessRelatedByParentid($criteria = null, PropelPDO $con = null)
	{
		if ($criteria === null) {
			$criteria = new Criteria(CategoriesPeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collCategoriessRelatedByParentid === null) {
			if ($this->isNew()) {
			   $this->collCategoriessRelatedByParentid = array();
			} else {

				$criteria->add(CategoriesPeer::PARENTID, $this->id);

				CategoriesPeer::addSelectColumns($criteria);
				$this->collCategoriessRelatedByParentid = CategoriesPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(CategoriesPeer::PARENTID, $this->id);

				CategoriesPeer::addSelectColumns($criteria);
				if (!isset($this->lastCategoriesRelatedByParentidCriteria) || !$this->lastCategoriesRelatedByParentidCriteria->equals($criteria)) {
					$this->collCategoriessRelatedByParentid = CategoriesPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastCategoriesRelatedByParentidCriteria = $criteria;
		return $this->collCategoriessRelatedByParentid;
	}

	
	public function countCategoriessRelatedByParentid(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
	{
		if ($criteria === null) {
			$criteria = new Criteria(CategoriesPeer::DATABASE_NAME);
		} else {
			$criteria = clone $criteria;
		}

		if ($distinct) {
			$criteria->setDistinct();
		}

		$count = null;

		if ($this->collCategoriessRelatedByParentid === null) {
			if ($this->isNew()) {
				$count = 0;
			} else {

				$criteria->add(CategoriesPeer::PARENTID, $this->id);

				$count = CategoriesPeer::doCount($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(CategoriesPeer::PARENTID, $this->id);

				if (!isset($this->lastCategoriesRelatedByParentidCriteria) || !$this->lastCategoriesRelatedByParentidCriteria->equals($criteria)) {
					$count = CategoriesPeer::doCount($criteria, $con);
				} else {
					$count = count($this->collCategoriessRelatedByParentid);
				}
			} else {
				$count = count($this->collCategoriessRelatedByParentid);
			}
		}
		$this->lastCategoriesRelatedByParentidCriteria = $criteria;
		return $count;
	}

	
	public function addCategoriesRelatedByParentid(Categories $l)
	{
		if ($this->collCategoriessRelatedByParentid === null) {
			$this->initCategoriessRelatedByParentid();
		}
		if (!in_array($l, $this->collCategoriessRelatedByParentid, true)) { 			array_push($this->collCategoriessRelatedByParentid, $l);
			$l->setCategoriesRelatedByParentid($this);
		}
	}

	
	public function clearProductss()
	{
		$this->collProductss = null; 	}

	
	public function initProductss()
	{
		$this->collProductss = array();
	}

	
	public function getProductss($criteria = null, PropelPDO $con = null)
	{
		if ($criteria === null) {
			$criteria = new Criteria(CategoriesPeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collProductss === null) {
			if ($this->isNew()) {
			   $this->collProductss = array();
			} else {

				$criteria->add(ProductsPeer::CATEGORY, $this->id);

				ProductsPeer::addSelectColumns($criteria);
				$this->collProductss = ProductsPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(ProductsPeer::CATEGORY, $this->id);

				ProductsPeer::addSelectColumns($criteria);
				if (!isset($this->lastProductsCriteria) || !$this->lastProductsCriteria->equals($criteria)) {
					$this->collProductss = ProductsPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastProductsCriteria = $criteria;
		return $this->collProductss;
	}

	
	public function countProductss(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
	{
		if ($criteria === null) {
			$criteria = new Criteria(CategoriesPeer::DATABASE_NAME);
		} else {
			$criteria = clone $criteria;
		}

		if ($distinct) {
			$criteria->setDistinct();
		}

		$count = null;

		if ($this->collProductss === null) {
			if ($this->isNew()) {
				$count = 0;
			} else {

				$criteria->add(ProductsPeer::CATEGORY, $this->id);

				$count = ProductsPeer::doCount($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(ProductsPeer::CATEGORY, $this->id);

				if (!isset($this->lastProductsCriteria) || !$this->lastProductsCriteria->equals($criteria)) {
					$count = ProductsPeer::doCount($criteria, $con);
				} else {
					$count = count($this->collProductss);
				}
			} else {
				$count = count($this->collProductss);
			}
		}
		$this->lastProductsCriteria = $criteria;
		return $count;
	}

	
	public function addProducts(Products $l)
	{
		if ($this->collProductss === null) {
			$this->initProductss();
		}
		if (!in_array($l, $this->collProductss, true)) { 			array_push($this->collProductss, $l);
			$l->setCategories($this);
		}
	}


	
	public function getProductssJoinTaxcategories($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN)
	{
		if ($criteria === null) {
			$criteria = new Criteria(CategoriesPeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collProductss === null) {
			if ($this->isNew()) {
				$this->collProductss = array();
			} else {

				$criteria->add(ProductsPeer::CATEGORY, $this->id);

				$this->collProductss = ProductsPeer::doSelectJoinTaxcategories($criteria, $con, $join_behavior);
			}
		} else {
									
			$criteria->add(ProductsPeer::CATEGORY, $this->id);

			if (!isset($this->lastProductsCriteria) || !$this->lastProductsCriteria->equals($criteria)) {
				$this->collProductss = ProductsPeer::doSelectJoinTaxcategories($criteria, $con, $join_behavior);
			}
		}
		$this->lastProductsCriteria = $criteria;

		return $this->collProductss;
	}

	
	public function clearAllReferences($deep = false)
	{
		if ($deep) {
			if ($this->collCategoriessRelatedByParentid) {
				foreach ((array) $this->collCategoriessRelatedByParentid as $o) {
					$o->clearAllReferences($deep);
				}
			}
			if ($this->collProductss) {
				foreach ((array) $this->collProductss as $o) {
					$o->clearAllReferences($deep);
				}
			}
		} 
		$this->collCategoriessRelatedByParentid = null;
		$this->collProductss = null;
			$this->aCategoriesRelatedByParentid = null;
	}

} 